package com.tanhua.service;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.api.db.UserInfoApi;
import com.tanhua.api.mongo.CommentApi;
import com.tanhua.api.mongo.PublishApi;
import com.tanhua.api.mongo.VideoApi;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.domain.mongo.PublishVo;
import com.tanhua.domain.mongo.Video;
import com.tanhua.domain.vo.CommentVo;
import com.tanhua.domain.vo.PageBeanVo;
import com.tanhua.domain.vo.VideoVo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserService {

    @Reference
    private UserInfoApi userInfoApi;

    //用户列表分页查询
    public ResponseEntity findUserAllPage(Integer page, Integer pagesize) {
            //mysql --    mybatis-plus插件
       IPage iPage= userInfoApi.findUserAllPage(page,pagesize);

       //页面需要的pageBean
        PageBeanVo pbvo = new PageBeanVo(page, pagesize, iPage.getTotal(), iPage.getRecords());
        return ResponseEntity.ok(pbvo);
    }

    //根据用户id查看该用户详情
    public ResponseEntity findUserById(Long userId) {

        UserInfo userInfo = userInfoApi.findUserInfoVo(userId);

        return ResponseEntity.ok(userInfo);
    }

    @Reference
    private VideoApi videoApi;

    //根据用户id分页查看该用户小视频
    public ResponseEntity findUserVideo(Integer page, Integer pagesize, Long uid) {

        PageBeanVo pbvo=videoApi.findVideoByUser(page,pagesize,uid);
        List<Video> items = (List<Video>) pbvo.getItems();

        List<VideoVo> list=new ArrayList<>();
        for (Video video : items) {
            //userInfo
            UserInfo userInfo = userInfoApi.findUserInfoVo(video.getUserId());
            //封装videoVo
            VideoVo videoVo = new VideoVo();
            BeanUtils.copyProperties(userInfo,videoVo);
            BeanUtils.copyProperties(video,videoVo);
            //手动封装
            videoVo.setId(video.getId().toString());
            videoVo.setCover(video.getPicUrl());
            videoVo.setSignature(video.getText());

            list.add(videoVo);
        }

        pbvo.setItems(list);
        return ResponseEntity.ok(pbvo);
    }

    @Reference
    private PublishApi publishApi;
    //根据用户id查看该用户发布过的圈子动态
    public ResponseEntity findQuanZi(Integer page, Integer pagesize, Long uid, Integer state) {

        // 查询该用户的圈子数据
        PageBeanVo pbvo=publishApi.findPublish(page,pagesize,uid,state);
        List<Publish> items = (List<Publish>) pbvo.getItems();

        List<PublishVo> list=new ArrayList<>();
        for (Publish publish : items) {
            // 查询userInfo
            UserInfo userInfo = userInfoApi.findUserInfoVo(publish.getUserId());
            //封装页面需要的publishVo对象
            PublishVo publishVo = new PublishVo();
            publishVo.setUserInfo(userInfo);
            publishVo.setPublish(publish);
            publishVo.setCreateDate(new DateTime(publish.getCreated()).toString("yyyy-MM-dd HH:mm"));
            list.add(publishVo);
        }
        pbvo.setItems(list);
        return ResponseEntity.ok(pbvo);
    }

    //根据用户id查看该用户指定的圈子动态
    public ResponseEntity findQuanZiById(String publishId) {

        Publish publish = publishApi.findByIdtoPublish(publishId);

        PublishVo publishVo = new PublishVo();
        UserInfo userInfo = userInfoApi.findUserInfoVo(publish.getUserId());
        publishVo.setUserInfo(userInfo);
        publishVo.setPublish(publish);
        publishVo.setCreateDate(new DateTime(publish.getCreated()).toString("yyyy-MM-dd HH:mm"));

            return ResponseEntity.ok(publishVo);
    }

    @Reference
    private CommentApi commentApi;

    //根据用户id查看该用户指定的圈子动态评论
    public ResponseEntity findQuanZiComment(Integer page, Integer pagesize, String messageID) {

        //查询quanzi_comment获取Comment数据
        PageBeanVo pbvo = commentApi.findCommentPage(page, pagesize, messageID,1);
        List<Comment> items = (List<Comment>) pbvo.getItems();

        List<CommentVo> list=new ArrayList<>();
        for (Comment comment : items) {
            //userInfo
            UserInfo userInfo = userInfoApi.findUserInfoVo(comment.getUserId());
            //封装CommentVo
            CommentVo commentVo = new CommentVo();
            BeanUtils.copyProperties(userInfo,commentVo);
            BeanUtils.copyProperties(comment,commentVo);
            list.add(commentVo);
        }
        pbvo.setItems(list);
        return ResponseEntity.ok(pbvo);
    }

    //动态复审通过
    public ResponseEntity commentAuditTrue(String[] param) {
        //设置审核结果为通过
        Integer state = 1;
        //遍历数组拿到publishId
        for (String publishId : param) {
            //修改审核状态
            publishApi.updateState(publishId,state);
        }
        return ResponseEntity.ok("操作成功");
    }

    //复审不通过
    public ResponseEntity commentAuditFalse(String[] param) {
        //设置审核结果为通过
        Integer state = 2;
        //遍历数组拿到publishId
        for (String publishId : param) {
            //修改审核状态
            publishApi.updateState(publishId,state);
        }
        return ResponseEntity.ok("操作成功");
    }
}
